233B - Non-square Equation - CodeForces Solution


binary search brute force math *1400

Please click on ads to support us..

Python Code:

import collections
from decimal import Decimal
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
from unicodedata import decimal
def valid(i,j,n,m):
        if i<n and i>=0 and j>=0 and j< m :return True         return  False
def sumn(i,n):
    return (n-i)*(i+n)/2
def sqfun(a,b,c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a
def getprime(num):
        if  all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):return  True



def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]



def solve():
    n=inp()
    for sm in range(1,91):
                sx = (Decimal((sm**2)+4*n).sqrt()-sm)/2
        if sx>0 and int(sx)==sx:
            if sm==sum([int(i) for i in str(int(sx))]):
                return int(sx)
    return -1
if __name__ == "__main__":
        print(solve())








C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define AYAALI ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define FOR(i, a, b) for(int i = int(a); i < int(b); i++)
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define ll long long
#define pb(x) push_back(x)
#define MP(x, y) make_pair(x,y)
#define fi first
#define se second
#define endl '\n'
#define all(v) v.begin(), v.end()
const double EPS = 1e-9;
const ll N=1e5+1;
//---------------------------------------------
/*void files(){
    freopen("pails.in", "r", stdin);
    freopen("pails.out", "w", stdout);
}*/

ll s(ll i){
    ll sum=0;
    // 99
    while(i>0){
        sum+=(i%10);
        i/=10;
    }
    return sum;
}
void solve()
{
    ll n;
    cin>>n;
    ll ans=0;
    // x biggest val 10^9 biggest sum 999999999
    // 1.53 - 1 = 0.53 *10 = 5.3
    for(ll i =1;i<=100;i++){
        ll in = (-i+sqrt(pow(i,2)+(4*n)))/2;
        double de = (-i+sqrt(pow(i,2)+(4*n)))/2;
        if(in>0 && (de-in)*1000000000==0 ){
             if(i == s((-i+sqrt(pow(i,2)+(4*n)))/2)){ans=(-i+sqrt(pow(i,2)+(4*n)))/2;break;}
        }
    }
    if(ans)cout<<ans;
    else cout<<-1;





}


int main() {
    AYAALI
    ll t=1;
    //files();
    //cin>>t;
    while(t--){
        solve();}
}


Comments

Submit
0 Comments
More Questions

1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa